package com.augurit.wf.jbpmex.task.multitask.completerule;

import java.util.List;

import org.jbpm.api.history.HistoryTask;
import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.common.base.form.ResultForm;
import com.augurit.wf.jbpmex.task.multitask.completerule.base.BaseCompleteRule;

public class ByCountRule extends BaseCompleteRule {

	private int completeCount;         //要求完成个数
	
	public ByCountRule(int completeCount){
		this.completeCount = completeCount;
	}
	
	public void init(TaskImpl curTask, TaskImpl superTask, List<HistoryTaskImpl> hisChildTasks) {
		super.initParams(curTask, superTask, hisChildTasks);
	}
	
	@Override
	public boolean isEnd() {
		int count = 0;
		
		if(hisChildTasks != null && hisChildTasks.size() >= completeCount){
			for(HistoryTaskImpl hisChildTask : hisChildTasks){
				if(hisChildTask.getState() != null 
						&& hisChildTask.getState().equals(HistoryTask.STATE_COMPLETED)){
					count++;
				}
			}
		}
		
		if(count >= completeCount)
			return true;
		
		return false;
	}

	@Override
	public ResultForm validate(String[] userKeys) {
		ResultForm result = new ResultForm(true);
		
		if(userKeys.length < completeCount){
			result.setSuccess(false);
			result.setErrMsg("用户数必须大于" + completeCount);
		}
		
		return result;
	}
}
